package com.example.administrator.wallet.view.activity.mine.Msg;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.view.fragment.mine.InformFragment;
import com.example.administrator.wallet.view.fragment.mine.NoticeFragment;

import butterknife.Bind;
import butterknife.OnClick;

public class MsgActivity extends BaseActivity {


    @Bind(R.id.tv_notice)
    TextView mTv_notice;
    @Bind(R.id.line_notice)
    TextView mLine_nptice;
    @Bind(R.id.msg_notice)
    RelativeLayout mMsg_notice;

    @Bind(R.id.tv_inform)
    TextView mTv_inform;
    @Bind(R.id.line_inform)
    TextView mLine_inform;
    @Bind(R.id.msg_inform)
    RelativeLayout mMsg_inform;

    @Bind(R.id.finish_img)
    ImageButton mIb_back;
    @Bind(R.id.view_title)
    TextView mV_title;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private NoticeFragment noticeFragment;//一个页面
    private InformFragment informFragment;//另一个

    @Override
    public void init() {
        setStatuColor();
        inittitle();
        selected(0);
    }

    private void inittitle(){

        mIb_back.setVisibility(View.VISIBLE);
        mV_title.setText(R.string.news);

        mIb_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            onBackPressed();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    private void selected(int i) {
        resetImage();
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                if (noticeFragment == null) {
                    noticeFragment = new NoticeFragment();
                    transaction.add(R.id.main_container, noticeFragment);
                } else {
                    transaction.show(noticeFragment);
                }
                transaction.commit();
                mTv_notice.setTextColor(getResources().getColor(R.color.button_seletor));
                mLine_nptice.setBackgroundColor(getResources().getColor(R.color.button_seletor));
                break;
            case 1:

                if (informFragment == null) {
                    informFragment = new InformFragment();
                    transaction.add(R.id.main_container, informFragment);
                } else {
                    transaction.show(informFragment);
                }
                transaction.commit();
                mTv_inform.setTextColor(getResources().getColor(R.color.button_seletor));
                mLine_inform.setBackgroundColor(getResources().getColor(R.color.button_seletor));
                break;
        }
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (noticeFragment != null) {
            transaction.hide(noticeFragment);
        }
        if (informFragment != null) {
            transaction.hide(informFragment);
        }
    }

    private void resetImage() {
        mTv_notice.setTextColor(getResources().getColor(R.color.button_normal2));
        mTv_inform.setTextColor(getResources().getColor(R.color.button_normal2));
        mLine_nptice.setBackgroundColor(getResources().getColor(R.color.colorToolbarBack));
        mLine_inform.setBackgroundColor(getResources().getColor(R.color.colorToolbarBack));
    }


    @Override
    public int getLayoutResource() {
        return R.layout.activity_msg;
    }

    @OnClick({R.id.msg_notice, R.id.msg_inform})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.msg_notice:
                selected(0);
                break;
            case R.id.msg_inform:
                selected(1);
                break;
        }
    }
}

